/*
 * Copyright (C) 2023 KylinSoft Co., Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
**/
#include "klabel.h"
#include <QFont>
#include <QApplication>
#include <QToolTip>
#include <QDesktopWidget>
#include <QDebug>

KLabel::KLabel(QWidget *parent)
    : QLabel(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    //m_strText = "";
     sysFont = qApp->font();

     QPalette pe;
     pe.setColor(QPalette::WindowText,Qt::white);
     this->setPalette(pe);
}

void KLabel::setFontSize(int fontSize)
{
    sysFont.setPointSize(fontSize);
    this->setFont(sysFont);
}

void KLabel::setFontFamily(QString &family)
{
    sysFont.setFamily(family);
    this->setFont(sysFont);
}

void KLabel::setTipText(const QString &strText)
{
    m_strText = strText;
    QLabel::setText(strText);
}

void KLabel::setText(const QString &strText)
{
    setTipText(strText);
}

void KLabel::clear()
{
    m_strText.clear();
    QLabel::clear();
}

QString KLabel::text() const
{
    if (m_strText.isEmpty())
        return QLabel::text();
    else
        return m_strText;
}

QString KLabel::getElidedText(QFont font,int width,QString strInfo)
{
    QFontMetrics fontMetrics(font);
    //如果当前字体下，字符串长度大于指定宽度
    if(fontMetrics.width(strInfo) > width)
    {
        strInfo= QFontMetrics(font).elidedText(strInfo, Qt::ElideRight, width);
    }
    return strInfo;
}

void KLabel::paintEvent(QPaintEvent *event)
{
    if (m_strText.isEmpty() && !QLabel::text().isEmpty())
        m_strText = QLabel::text();
    QString strEText = getElidedText(font(),  width() ,m_strText);
    if (strEText == m_strText) {
        QLabel::setText(m_strText);
    } else if (strEText != m_strText ) {
        QLabel::setText(strEText);
    }
    QLabel::paintEvent(event);
}
